<template>
  <div class="allsearch">
    <section class="search-content">
      <el-row :gutter="4" type="flex" justify="start">
        <el-col :span="24">
          <el-input
            v-model="username"
            style="width:200px;"
            placeholder="用户名"
            @keyup.enter.native="handleFilter"
          />
          <el-date-picker
            v-model="cycle"
            :picker-options="picker"
            type="daterange"
            align="right"
            unlink-panels
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            style="width:400px;"
            @change="handleFilter"
          />
          <el-button type="primary" size="medium" @click="handleFilter">搜索</el-button>
          <el-button size="medium" type="info" @click="clearParams">清空</el-button>
          <el-button :loading="downloadLoading" size="medium" style="background-color:#e9ab3e;color:#fff;" @click="handleExport">导出</el-button>
        </el-col>
      </el-row>
    </section>
    <subtable
      :table-label="inviteLabel"
      :table-data="listFilter.slice((currentpage-1)*pagesize,currentpage*pagesize)"
    />
    <pagination
      :total="total"
      :current-page="currentpage"
      :page-size="pagesize"
      @handleSizeChange="handleSizeChange"
      @handleCurrentChange="handleCurrentChange"
    />
  </div>
</template>

<script>
import Subtable from '@/components/Eltable'
import Pagination from '@/components/Pagination' // 分页组件
import { getInviteRecord } from '@/api/operate/invitation' // 数据接口
import { getTenantId } from '@/utils/auth'
export default {
  components: {
    Subtable,
    Pagination
  },
  data() {
    return {
      downloadLoading: false,
      username: '',
      cycle: [],
      total: 0,
      currentpage: 1,
      pagesize: 10,
      listFilter: [],
      picker: {
        shortcuts: [{
          text: '最近一周',
          onClick(picker) {
            const end = new Date()
            const start = new Date()
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 7)
            picker.$emit('pick', [start, end])
          }
        }, {
          text: '最近一个月',
          onClick(picker) {
            const end = new Date()
            const start = new Date()
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 30)
            picker.$emit('pick', [start, end])
          }
        }, {
          text: '最近三个月',
          onClick(picker) {
            const end = new Date()
            const start = new Date()
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 90)
            picker.$emit('pick', [start, end])
          }
        }]
      },
      inviteLabel: [
        {
          label: '邀请人', width: '', prop: 'inviteUserName'
        }, {
          label: '注册用户', width: '', prop: 'invitedUserName'
        }, {
          label: '订单消费总额', width: '', prop: 'amount'
        }, {
          label: '订单虚拟币总额', width: '', prop: 'coinAmount'
        }, {
          label: '订单现金总额', width: '', prop: 'cashAmount'
        }, {
          label: '邀请码', width: '', prop: 'code'
        }, {
          label: '邀请时间', width: '', prop: 'inviteTimeTimeStr'
        }
      ],
      inviteData: []
    }
  },
  mounted() {
    this.getData()
  },
  methods: {
    handleSizeChange(val) {
      console.log(val)
      this.currentpage = 1
      this.pagesize = val.value
      if (this.pagesize >= 20) {
        scrollTo(50, 800)
      }
    },
    handleCurrentChange(val) {
      console.log(val)
      this.currentpage = val.value
      if (this.pagesize >= 20) {
        scrollTo(50, 800)
      }
    },
    handleFilter() {
      this.listFilter = this.inviteData.filter(item => {
        if (this.username && item.invitedUserName.toLowerCase().indexOf(this.username.toLowerCase()) === -1) return false
        if (this.cycle && (new Date(item.inviteTime) < this.cycle[0] || new Date(item.inviteTime) > this.cycle[1])) return false
        return true
      })
      this.total = this.listFilter.length
    },
    clearParams() {
      this.username = ''
      this.cycle = []
      this.getData()
    },
    getData() {
      getInviteRecord(getTenantId()).then(response => {
        this.inviteData = response.data.items.filter(item => {
          if (item.tenantId === getTenantId()) return true
        })
        this.total = this.inviteData.length
        this.listFilter = this.inviteData
      })
    },
    // 导出按钮
    handleExport() {
      this.downloadLoading = true
      import('@/vendor/Export2Excel').then(excel => {
        const tHeader = ['邀请人', '注册用户', '订单消费总额', '订单虚拟币总额', '订单现金总额', '邀请码', '邀请时间']
        const filterVal = ['inviteUserName', 'invitedUserName', 'amount', 'coinAmount', 'cashAmount', 'code', 'inviteTimeStr']
        const list = this.inviteData
        const data = this.formatJson(filterVal, list)
        excel.export_json_to_excel({
          header: tHeader,
          data,
          filename: 'recordList',
          autoWidth: true,
          bookType: 'xlsx'
        })
        this.downloadLoading = false
      })
    },
    formatJson(filterVal, jsonData) {
      return jsonData.map(v => filterVal.map(j => {
        return v[j]
      }))
    }
  }
}
</script>

<style lang="scss" scoped>
.allsearch {
  margin-top: 0;
  padding: 0;
  .search-content {
    width: 100%;
  }
}
</style>
